/*
 * Cliente_Viewfi2.java
 *
 * Created on 20 de mayo de 2008, 18:55
 */

package propres;
import java.util.*;
import javax.swing.DefaultListModel;
import java.awt.Color;
import javax.swing.plaf.metal.*;
import javax.swing.UIManager;
/**
 *
 * @author  neochange
 */
public class Cliente_Viewfi2 extends javax.swing.JPanel  {
    
    /** Creates new form Cliente_Viewfi2 */
    public Cliente_Viewfi2(){
        
        initComponents();
    }
    
    public Cliente_Viewfi2(CtrlVistaCliente CVC){
        try{
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        }
        catch(Exception e){
        System.out.println("Error cargando L&F: "+e.getMessage());
        }
        
        initComponents();
        CVc = CVC;
        L1 = new DefaultListModel();
        jList1.setModel(L1);
        }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        jFrame1.setName("jFrame1"); // NOI18N

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setName("Gestion Cliente"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(propres.PropresApp.class).getContext().getResourceMap(Cliente_Viewfi2.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jTextField4.setText(resourceMap.getString("jTextField4.text")); // NOI18N
        jTextField4.setName("jTextField4"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dar Alta", "Consultar Persona", "Modificar Persona", "Eliminar Persona" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList1.setName("jList1"); // NOI18N
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
                        .addGap(118, 118, 118))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addGap(66, 66, 66)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(377, 377, 377))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3))
                            .addComponent(jButton2))))
                .addContainerGap(114, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Gestion de acciones
        
        String no,ap,dn,dom;
        String[] T = new String[4];
        no = jTextField1.getText(); // Obtengo los valores de los cuadros
        ap = jTextField2.getText();
        dn = jTextField3.getText();
        dom = jTextField4.getText();
        
        
        String accion = (String)jComboBox1.getSelectedItem(); // Obtengo el Combobox
        
        
        String[] estado = new String[4];
        try{
        estado = CVc.comprovacion(no, ap, dn, dom, accion);
        }
        catch(Exception e){System.out.println("Error comprovando el cliente");}
        
                
        if(estado[0].equals("ok") && estado[1].equals("ok") && estado[2].equals("ok") && estado[3].equals("ok")){
        
            try{
            T=CVc.gestion_evento(accion,no,ap,dn,dom);
            }
            catch(Exception e){
                Dialog_error u = new Dialog_error(jFrame1,true,e.getMessage());
                u.setLocationRelativeTo(null);
                u.setVisible(true);
            }
            
            if(accion.equals("Consultar Persona")){
                if(T[0]==null && T[1]==null && T[2]==null && T[3]==null){
                L1.addElement("El cliente no está en la estructura");
                }
            }
            jTextField1.setText(T[0]); // actualizar estado jTextField
            jTextField2.setText(T[1]);
            jTextField3.setText(T[2]);
            jTextField4.setText(T[3]);
            
        }
        else{ // Error en las operaciones
           
            String z="";
            Dialog_error2 u = new Dialog_error2(jFrame1,true,estado);
            u.setLocationRelativeTo(null);
            u.setVisible(true);
        
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // Consultar clientes:
        
        List<String> T = new ArrayList();
        try{
        T=CVc.listar_cliente();
        }
        catch(Exception e){
            System.out.println("Error listando los clientes");
            Dialog_error u = new Dialog_error(jFrame1,true,"Error listando los clientes");
            u.setLocationRelativeTo(null);
            u.setVisible(true);
        }
        L1.clear();
        
        int i = 0;
        if(!T.isEmpty()){
            while(i<T.size()){
            L1.addElement(T.get(i));
            i++;
            }
        }
        else{
        L1.clear();
        L1.addElement("No hay clientes en el sistema");
        }
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // Cargar / Guardar estructura de clientes
        L1.clear();
        
        if(evt.getActionCommand().equals("Guardar Disco")){
            try{
                
            Confpath G = new Confpath(jFrame1,true);
            G.setLocationRelativeTo(null);
            G.setVisible(true);
            
            if(!G.get_p().equals("Cancelo")){ 
                CVc.guardar_objeto(G.get_p());
                L1.addElement("Clientes guardados correctamente");
            }
            
            }
            catch(Exception e){
                System.out.println("Error de disco: "+e.getMessage());
                Dialog_error u = new Dialog_error(jFrame1,true,e.getMessage());
                u.setLocationRelativeTo(null);
                u.setVisible(true);
            }
        }
        else{
            try{
            
            Confpath G = new Confpath(jFrame1,true);
            G.setLocationRelativeTo(null);
            G.setVisible(true);

            if(!G.get_p().equals("Cancelo")){ 
                CVc.cargar_objeto(G.get_p());
                L1.addElement("Clientes cargados correctamente");
            }
            }
            catch(Exception e){
                System.out.println("Error de disco: "+e.getMessage());
                Dialog_error u = new Dialog_error(jFrame1,true,"Error cargando clientes: fichero dañado o inexistente");
                u.setLocationRelativeTo(null);
                u.setVisible(true);
            }
        
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        // Evento de la lista:
        if(!jList1.isSelectionEmpty()){
        String[] t = ((String)L1.getElementAt(jList1.getSelectedIndex())).split(" ");
        jTextField3.setText(t[0]);
        jList1.clearSelection();
        }
    }//GEN-LAST:event_jList1ValueChanged

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // Estado casillas
        
        if(evt.getItem().equals("Consultar Persona")){
        jTextField1.setEditable(false);
        jTextField1.setBackground(Color.LIGHT_GRAY);
        jTextField2.setEditable(false);
        jTextField2.setBackground(Color.LIGHT_GRAY);
        jTextField3.setEditable(true);
        jTextField3.setBackground(Color.white);
        jTextField4.setEditable(false);
        jTextField4.setBackground(Color.LIGHT_GRAY);
        
        }
        else if(evt.getItem().equals("Dar Alta")){
        jTextField1.setEditable(true);
        jTextField1.setBackground(Color.white);
        jTextField2.setEditable(true);
        jTextField2.setBackground(Color.white);
        jTextField3.setEditable(true);
        jTextField3.setBackground(Color.white);
        jTextField4.setEditable(true);
        jTextField4.setBackground(Color.white);
        
        }
        else if(evt.getItem().equals("Modificar Persona")){
        jTextField1.setEditable(true);
        jTextField1.setBackground(Color.white);
        jTextField2.setEditable(true);
        jTextField2.setBackground(Color.white);
        jTextField3.setEditable(true);
        jTextField3.setBackground(Color.white);
        jTextField4.setEditable(true);
        jTextField4.setBackground(Color.white);
        
        }
        else if(evt.getItem().equals("Eliminar Persona")){
        jTextField1.setEditable(false);
        jTextField1.setBackground(Color.LIGHT_GRAY);
        jTextField2.setEditable(false);
        jTextField2.setBackground(Color.LIGHT_GRAY);
        jTextField3.setEditable(true);
        jTextField3.setBackground(Color.white);
        jTextField4.setEditable(false);
        jTextField4.setBackground(Color.LIGHT_GRAY);
        
        }
        
    }//GEN-LAST:event_jComboBox1ItemStateChanged
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration//GEN-END:variables
    private CtrlVistaCliente CVc;
    private DefaultListModel L1;
    
    
}
